/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：服务                                                   
*│　作    者：sys                                              
*│　创建时间：2020-12-02 14:55:26   
*│　说    明：代码生成器生成   
*└──────────────────────────────────────────────────────────────┘
*/
using AutoMapper;
using SXFramWork.Core;
using SXFramWork.Model;
using SXFramWork.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SXFramWork.Service
{
    /// <summary>
	/// SysDictionary服务层
	/// </summary>
    public class SysDictionaryService: ISysDictionaryService
    {
        private readonly ISysDictionaryRepository _repository;
        private readonly IMapper _mapper;
        public SysDictionaryService(ISysDictionaryRepository repository,IMapper mapper)
        {
            _mapper = mapper;
            _repository = repository;
        }

        public async Task<IApiResult> Query(string name)
        {
           var list=  await _repository.GetAllAsync();
           if(name.NotNull())
            {
                list = list.Where(a => a.Name.Contains(name));
            }
           list = list.OrderBy(a => a.Sort);
           return ApiResult.Ok(list);
        }

        public async Task<IApiResult> QueryParents()
        {
            var res = await _repository.GetAllAsync();
            var list = res.Where(a =>a.ParentId == null).Select(m=>new {m.Id,m.Name });
            return ApiResult.Ok(list);
        }

        public async Task<IApiResult> Add(DictionaryAddModel model)
        {
            var entity = _mapper.Map<SysDictionaryEntity>(model);
            entity.Id = GuidHelper.NewSequentialGuid(SequentialGuidType.SequentialAtEnd);
            entity.CreateTime = DateTime.Now;

            var id=  await _repository.InsertAsync(entity);
            if (id.NotNull())
            {
                return ApiResult.Ok(id);
            }
            else
            {
                return ApiResult.NotOk("添加字典失败");
            }

        }

        public async Task<IApiResult> Delete(List< string> ids)
        {
            var res = await _repository.DeleteManyAsync(ids, "sysdictionary");
            if (res > 0)
                return ApiResult.Ok(res, "成功删除" + res + "条数据");
            else
                return ApiResult.NotOk("删除失败");
        }

        public async Task<IApiResult> GetById(string id)
        {
            if (id.IsNull()) return ApiResult.NotOk("参数【id】错误");
            var res= await _repository.GetAsync(id);
            res.Parent = new List<SysDictionaryEntity>();
            if (res.ParentId.NotNull()&&res.ParentId!="0")
            {
                var parent = await _repository.GetAsync(res.ParentId);
                parent.Selected = true;
                res.Parent.Add(parent);
              
            }
            //后续修改
            var res2 = await _repository.GetAllAsync();
            var list = res2.Where(a => a.ParentId == null && a.Id != res.ParentId);
            res.Parent.AddRange(list);
            return ApiResult.Ok(res);
        }

        public async Task<IApiResult> Update(DictionaryUpdateModel model)
        {
            var entity = _mapper.Map<SysDictionaryEntity>(model);
            var res=  await _repository.UpdateAsync(entity);
            if (res)
                return ApiResult.Ok("更新成功");
            else
                return ApiResult.NotOk("更新失败");
        }
    }
}